﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class User_UserEmail_EmailToShow : System.Web.UI.Page {
    private int emailId;
    private string type;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            if(Request["type"] != null) {
                this.type = Request["type"];
            } else {
                this.type = "incept";
            }
            if(null != Request["eid"]) {
                this.emailId = int.Parse(Request["eid"]);
                EmpEmailMappingtbService empEmailMappingtbService = new EmpEmailMappingtbService();
                empEmailMappingtbService.updateEmailReadState(emailId, type);
                ViewState["emailId"] = emailId;
            }
            this.emailInfoShow();
        }
    }

    protected void emailInfoShow() {
        if(emailId != 0) {
            EmpEmailMappingtbService empEmailMappingtbService = new EmpEmailMappingtbService();
            EmpEmailMappingtb empEmailMapping = empEmailMappingtbService.findByEmpEmailId(this.emailId, "incept");
            if(empEmailMapping != null && empEmailMapping.SendUser != null)
            {
                this.lblSendDateTime.Text = empEmailMapping.SendDateTime.ToString();
                if("incpet".Equals(this.type))
                {
                    this.Label2.Text = "接收人:";
                    this.lblSendUserName.Text = empEmailMapping.InceptUser.UserName;
                }
                else
                {
                    this.lblSendUserName.Text = empEmailMapping.SendUser.UserName;
                }
                this.ftbEmailContent.value = empEmailMapping.EmialTextContent;
                List<FileUploadMappingtb> affixFiles = empEmailMapping.AffixFiles;
                DataTable dt = new DataTable();
                dt.Columns.Add("affixFileName", typeof(string));
                dt.Columns.Add("affixFIlePath", typeof(string));
                foreach(FileUploadMappingtb affixFile in affixFiles)
                {
                    DataRow dr = dt.NewRow();
                    string fullFileName = affixFile.FilePath.Substring(affixFile.FilePath.LastIndexOf('\\') + 1);
                    string fileName = fullFileName.Substring(fullFileName.IndexOf('_') + 1);
                    dr["affixFileName"] = fileName;
                    dr["affixFilePath"] = affixFile.FilePath.Replace(FindConfigurationPath.instance().InformFilePath, "");
                    dt.Rows.Add(dr);
                }
                this.dltAffixFiles.DataSource = dt.DefaultView;
                this.dltAffixFiles.DataBind();
            }
            else
            {
                User curLoginUser = (User) Session["user"];
                emailId = (int) ViewState["emailId"];
                MessageAwoke.Instance().RemoveValueFromKeyForEmai(curLoginUser.LoginName, emailId);
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('邮件不存在或者已被删除！');window.close();", true);
            }
        }
    }
    protected void btnRetu_ServerClick(object sender, EventArgs e) {
        Response.Redirect("EmailInfoShow.aspx?type=" + this.type);
    }
    protected void dltAffixFiles_ItemDataBound(object sender, DataListItemEventArgs e) {
        if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {
            string filePath = ((HtmlInputHidden) e.Item.FindControl("hidFilePath")).Value;
            string fileName = filePath.Substring(filePath.IndexOf('_') + 1);
            ((LinkButton) e.Item.FindControl("lbtDownLoad")).Attributes.Add("filePath", filePath);
            ((LinkButton) e.Item.FindControl("lbtDownLoad")).Attributes.Add("fileName", fileName);
        }
    }
    protected void lbtDownLoad_Click(object sender, EventArgs e) {
        string filePath = FindConfigurationPath.instance().InformFilePath + ((LinkButton) sender).Attributes["filePath"].ToString();
        string fileName = ((LinkButton) sender).Attributes["fileName"].ToString();
        FileInfo fi = new FileInfo(filePath);
        DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath("fileupload_mail"));
        if (!dirinfo.Exists)
        {
            dirinfo.Create();
        }
        FileInfo[] fis = dirinfo.GetFiles();
        if (fis != null)
        {
            for (int i = 0; i < fis.Length; i++)
            {
                fis[i].Delete();
            }
        }
        fi.CopyTo(Server.MapPath("fileupload_mail/") + fileName, true);
        Response.Redirect("fileupload_mail/" + fileName);
        //if(fi.Exists)
        //{
        //    Response.Clear();
        //    Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        //    Response.ContentType = "application/octet-stream";
        //    Response.Filter.Close();
        //    Response.WriteFile(fi.FullName);
        //    Response.End();
        //}
    }
    protected void Button1_Click(object sender, EventArgs e) {
        if(ViewState["emailId"] != null) {
            User curLoginUser = (User)Session["user"];
            emailId = (int) ViewState["emailId"];
            MessageAwoke.Instance().RemoveValueFromKeyForEmai(curLoginUser.LoginName, emailId);
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.close();", true);
        }
    }
}
